package com.ztgf.shopping.service.shop;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.shop.ShopCategoryRelationAuditMapper;
import com.ztgf.shopping.models.bo.shop.ShopCategoryRelationAuditSaveBO;
import com.ztgf.shopping.models.bo.shop.ShopCategoryRelationAuditSaveListBO;
import com.ztgf.shopping.models.bo.shop.ShopCategoryRelationSaveBO;
import com.ztgf.shopping.models.dto.shop.*;
import com.ztgf.shopping.models.service.shop.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 店铺品牌分类审核控制器
 *
 * @author zhanghualang
 * @Date 2020-06-24 11:11:48
 */
@DubboService
public class ShopCategoryRelationAuditServiceImpl extends ServiceImpl<ShopCategoryRelationAuditMapper, ShopCategoryRelationAuditDTO> implements IShopCategoryRelationAuditService{

    @Resource
    private YmlConfig ymlConfig;
    @Autowired
    @Lazy
    private IShopInfoService iShopInfoService;
    @Autowired
    private IShopInfoAuditService iShopInfoAuditService;
    @Autowired
    private IShopInfoAuditLogService iShopInfoAuditLogService;
    @Autowired
    private IShopBrandRelationAuditService iShopBrandRelationAuditService;
    @Autowired
    private IShopCategoryRelationAuditService iShopCategoryRelationAuditService;

    public Integer getCategoryAuditNum(Long shopId,Long brandId){
        Wrapper<ShopCategoryRelationAuditDTO> wrapper = new EntityWrapper<>();
        //"是否审核 0未审核、1打回、2审核通过、3审核失败"
        //类型 0注册，1开店，2资质变更，3新增品牌，4新增类目"
        wrapper.eq("shop_id", shopId).eq("brand_id", brandId).eq("status",0);
        wrapper.andNew().eq("type", 4).or().eq("type", 3);
        return selectList(wrapper).size();
    }

    @Override
    public boolean add(ShopCategoryRelationAuditSaveBO saveBO) {
        ShopCategoryRelationAuditDTO dto = new ShopCategoryRelationAuditDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public List<ShopCategoryRelationAuditDTO> queryListByShopAudit(Long shopAuditId, Long brandId, Integer status, Integer type) {
        Wrapper<ShopCategoryRelationAuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_audit_id", shopAuditId).eq("brand_id", brandId).eq("status", status).eq("type", type);
        return this.selectList(wrapper);
    }

    @Override
    @Transactional
    public boolean addCategory(ShopCategoryRelationAuditSaveListBO bo) {
        //debug 事务预留
        //审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
        /**
         * 1增加店铺审核表
         * 2增加日志表
         * 3增加关联关系审核表
         * 4增加分类审核表
         */
        Long userId = bo.getCreateUserId();
        Long shopId = bo.getShopId();

        //增加店铺审核表
        Long shopInfoAuditId = addShopInfoAudit(shopId, userId, 4);
        //增加日志表
        addAuditLog(shopInfoAuditId, shopId, userId, 4);
        //增加关联关系审核表
        addBrandRelationAudit(shopInfoAuditId, bo.getBrandId(), shopId, userId, 4);

        /**
         * 保存品牌分类审核表
         */
        List<ShopCategoryRelationSaveBO> shopCategoryRelationSaveBOList = JSON.parseArray(bo.getShopCategoryRelationSaveBOList(), ShopCategoryRelationSaveBO.class);
        //foreach品牌分类
        for (ShopCategoryRelationSaveBO shopCategoryRelationSaveBO : shopCategoryRelationSaveBOList){
            ShopCategoryRelationAuditDTO shopCategoryRelationAuditDTO = new ShopCategoryRelationAuditDTO();
            shopCategoryRelationAuditDTO.setId(ymlConfig.getPkId());
            shopCategoryRelationAuditDTO.setCreateUserId(userId);
            shopCategoryRelationAuditDTO.setShopAuditId(shopInfoAuditId);
            shopCategoryRelationAuditDTO.setShopId(shopId);
            shopCategoryRelationAuditDTO.setBrandId(bo.getBrandId());
            shopCategoryRelationAuditDTO.setCategoryId(shopCategoryRelationSaveBO.getCategoryId());
            shopCategoryRelationAuditDTO.setType(4);//审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
            shopCategoryRelationAuditDTO.setStatus(0);//审核状态 0审核中，1审核通过，2审核打回，3审核不通过
            iShopCategoryRelationAuditService.insert(shopCategoryRelationAuditDTO);
        }

        return true;
    }


    //新增店铺审核表
    private Long addShopInfoAudit(Long shopId, Long userId, Integer type){
        ShopInfoDTO shopInfoDTO = iShopInfoService.selectById(shopId);
        //新增店铺审核表
        ShopInfoAuditDTO shopInfoAuditDTO = new ShopInfoAuditDTO();
        Long shopInfoAuditId = ymlConfig.getPkId();
        shopInfoAuditDTO.setId(shopInfoAuditId);
        shopInfoAuditDTO.setCreateUserId(userId);
        shopInfoAuditDTO.setShopId(shopId);
        shopInfoAuditDTO.setName(shopInfoDTO.getName());//公司名称
        shopInfoAuditDTO.setShopName(shopInfoDTO.getShopName());//店铺名称
        shopInfoAuditDTO.setShopAbbreviation(shopInfoDTO.getShopAbbreviation());//店铺简称
        shopInfoAuditDTO.setType(type);//审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
        shopInfoAuditDTO.setStatus(0);//审核状态 0审核中，1审核通过，2审核打回，3审核不通过
        iShopInfoAuditService.insert(shopInfoAuditDTO);
        return shopInfoAuditId;
    }

    private boolean addAuditLog(Long shopInfoAuditId, Long shopId, Long userId, Integer type){
        ShopInfoAuditLogDTO shopInfoAuditLogDTO = new ShopInfoAuditLogDTO();
        shopInfoAuditLogDTO.setId(ymlConfig.getPkId());
        shopInfoAuditLogDTO.setShopAuditId(shopInfoAuditId);
        shopInfoAuditLogDTO.setCreateUserId(userId);
        shopInfoAuditLogDTO.setShopId(shopId);
        shopInfoAuditLogDTO.setType(type);
        shopInfoAuditLogDTO.setStatus(0);
        return iShopInfoAuditLogService.insert(shopInfoAuditLogDTO);
    }

    private boolean addBrandRelationAudit(Long shopInfoAuditId, Long brandId, Long shopId, Long userId, Integer type){
        ShopBrandRelationAuditDTO shopBrandRelationAuditDTO = new ShopBrandRelationAuditDTO();
        shopBrandRelationAuditDTO.setId(ymlConfig.getPkId());
        shopBrandRelationAuditDTO.setCreateUserId(userId);
        shopBrandRelationAuditDTO.setShopId(shopId);
        shopBrandRelationAuditDTO.setShopAuditId(shopInfoAuditId);
        shopBrandRelationAuditDTO.setBrandId(brandId);
        shopBrandRelationAuditDTO.setBrandType(0);//品牌是否存在 0存在。1不存在 默认0
        shopBrandRelationAuditDTO.setStatus(0);//是否审核 0未审核、1打回、2审核通过、3审核失败
        shopBrandRelationAuditDTO.setType(4);//类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
        return iShopBrandRelationAuditService.insert(shopBrandRelationAuditDTO);
    }
}
